package weather_service;

import weather_service.weatherprovider.*;
import weather_service.weatherprovider.locationdatamodel.*;
import weather_service.weatherprovider.weatherdatamodel.*;
 
import java.io.*;

/**
 * This class tests all the functionality provided by the WeatherGateway class
 */
public class TestWeatherGateway {

  public String partnerID = "1187706295";
  public String licenseKey = "f0464aaf10c38a37";
  public int temperature;
  public String description;
  public int iconNo;
  public String location;
  public String humidity;

  public void weatherInfo() {

      WeatherConfigurationIF config = new DefaultWeatherConfigurationBean(
      DefaultWeatherConfigurationBean.defaultWeatherServer, partnerID, licenseKey);
      WeatherReport weather = new WeatherReport();
      WeatherGateway gateway = new WeatherGateway(config);

      try {
          weather = gateway.getFullForecast("swxx0007", 3);
      } catch (WeatherError weatherError) {
          weatherError.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      } catch (IOException e) {
          e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      }
      String weatherString;
      String[] weatherData;
      weatherString = weather.toString();
//      System.out.print(weather.toString());
      weatherData = weatherString.split("\n");
//      for(int i = 0; i < weatherData.length; i++)
//          System.out.println(i + "---" + weatherData[i]);
//      System.out.println(weatherData[8].split("=")[1]);
      double tempTemp;
       temperature = Integer.parseInt((weatherData[8].split("=")[1]).split("\r")[0]);
       tempTemp = (double) temperature;
       tempTemp = (5.0 / 9.0) * (tempTemp - 32);
       temperature = (int) tempTemp;
       description = weatherData[1].split("=")[1];
       iconNo = Integer.parseInt((weatherData[3].split("=")[1]).split("\r")[0]);
       humidity = weatherData[7].split("=")[1] + "%";
       location = weatherData[4].split("=")[1];
       System.out.println(temperature);
       System.out.println(description);
       System.out.println(iconNo);
       System.out.println(location);
       System.out.println(humidity);


 }
    public static void main(String[] args) {
        TestWeatherGateway twg = new TestWeatherGateway();
        twg.weatherInfo();
    }
//      System.out.printf(":::: WeatherReport ::::\n%s\n::::\n", weather);
      //:::::::::::::::::::::::::::::::::::::::::::::::
    // testing "search" service
    //:::::::::::::::::::::::::::::::::::::::::::::::
   /*
    System.out.println("\n:: looking up location 20166 :: should work ok");
    gateway.testSearchService("20166");

    System.out.println("\n:: looking up location 20166x :: should work ok, and return empty SearchLocations");
    gateway.testSearchService("20166x");

    System.out.println("\n:: looking up location amsterdam :: should work ok, and return multiple SearchLocations");
    gateway.testSearchService("amsterdam");

    System.out.println("\n:: looking up location amsterdam :: should work ok, and return a single SearchLocation");
    gateway.testSearchService("la jolla");

    //:::::::::::::::::::::::::::::::::::::::::::::::
    // testing "local" service
    //:::::::::::::::::::::::::::::::::::::::::::::::
    System.out.println("\n:: checking 20166x weather :: WeatherErorr exception should be thrown");
    gateway.testLocalService("20166x", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking 20166 weather :: should work ok ");
    gateway.testLocalService("95051", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking BGXX0014 weather :: should work ok");
    gateway.testLocalService("BGXX0014", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking \"\" weather :: WeatherError exception should be thrown");
    gateway.testLocalService("", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking invalid locationID weather :: WeatherError exception should be thrown");
    gateway.testLocalService("asdfadf", DefaultWeatherConfigurationBean.defaultWeatherServer);

    System.out.println("\n:: checking invalid server weather :: IOException should be thrown ");
    gateway.testLocalService("asdfadf", "http://afdjalkfjefi");
  }

  public void testSearchService( String where ){
    WeatherGateway gateway = WeatherGateway.getDefaultInstance();

    try {
      SearchLocations locations = gateway.searchForLocations( where );

      System.out.printf(":::: SearchLocations ::::\n%s\n::::\n", locations);

    } catch (WeatherError e) {
      System.out.printf("---- Exception reported from parsing weather/response, %s ----\n", e);
    } catch (IOException e) {
      System.out.printf("---- Network connection problem or invalid server address, %s ----\n", e);
    }
  }

  public void testLocalService(String locationID, String weatherServer) {
    WeatherConfigurationIF config = new DefaultWeatherConfigurationBean(
        weatherServer, partnerID, licenseKey);
    WeatherGateway gateway = new WeatherGateway(config);

    WeatherReport weather = new WeatherReport();

    // multiple calls to exercise the cache
    for (int i = 0; i < 1; ++i) {
      try {
        weather = gateway.getFullForecast(locationID, 3);
        //weather = gateway.getLocation(locationID);
        //weather = gateway.getCurrentConditions(locationID);

        if (weather == null) {
          System.out.printf("locationID [%s] is not valid or no xml data returned, weather is null\n\n", locationID);
          return;
        }

        System.out.printf(":::: WeatherReport ::::\n%s\n::::\n", weather);

      } catch (IOException e) {
        System.out.printf("---- Network connection problem or invalid server address, %s ----\n", e);
        return;
      } catch (WeatherError e){
        System.out.printf("---- Exception reported from parsing weather/response, %s ----\n", e);
        return;
      }
    }
      */
 

}//end class TestWeatherGateway

